// gameServer/demo/config_demo.go
package main

import (
	"gameServer/cluster/gate"
	"gameServer/config"
	"gameServer/env"
	"gameServer/log"
	"time"
)

func main4() {
	// 首先初始化日志系统
	log.InitFromConfig(&log.LogConfig{
		Level:    "debug",
		Format:   "text",
		Output:   "stdout",
		FilePath: "logs/demo.log",
	})

	// 然后才能使用日志函数
	log.Info("Starting config demo...")

	// 创建文件配置
	fileConfig := config.NewFileConfig("config.json")
	if err := fileConfig.Load(); err != nil {
		log.Fatalf("Failed to load config: %v", err)
	}

	// 创建环境变量配置
	envConfig := env.NewEnvConfig("GAME")

	// 创建配置管理器
	manager := config.NewManager(envConfig, fileConfig)

	// 创建网关配置
	gateConfig := config.NewGateConfig()
	gateConfig.LoadFromManager(manager)

	log.Infof("Loaded gate config: addr=%s, port=%d",
		gateConfig.Server.Addr, gateConfig.Server.Port)

	// 创建网关组件
	gateComponent := gate.NewGateWithConfig(gateConfig)

	// 启动网关
	if err := gateComponent.Init(); err != nil {
		log.Fatalf("Failed to init gate: %v", err)
	}

	if err := gateComponent.Start(); err != nil {
		log.Fatalf("Failed to start gate: %v", err)
	}

	log.Info("Gateway started successfully")

	// 运行一段时间
	time.Sleep(30 * time.Second)

	// 停止网关
	if err := gateComponent.Stop(); err != nil {
		log.Errorf("Failed to stop gate: %v", err)
	}

	log.Info("Config demo completed")
}
